package demo1;

class MyCircularQueue {

    public int[] elem;
    int front;//头
    int rear;//尾

    public MyCircularQueue(int k) {
        elem = new int[k + 1];
    }

    //入队一个元素
    public boolean enQueue(int value) {
        //满了不能插
        if (isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    //出队一个元素
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    //获取队头元素
    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        //rear指向的是下一个位置,不是最后一个元素,如果rear=0,会越界
        if (rear == 0) {
            return elem[elem.length-2];
        }
        return elem[rear - 1];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return (front + 1) % elem.length == rear;
    }
}